'use strict';

angular.module('Common').controller('ScheduleRequestController',
    ['$scope', '$stateParams', 'BaseModule', 'BaseFunction', '$location', 'Authentication',
    function($scope, $stateParams, BaseModule, BaseFunction, $location, Authentication) {
        //$scope.authentication = Authentication;
        $scope.ScheduleStatusEnum = BaseFunction.getEnum('CommonEnum.ScheduleStatus');
        $scope.RowStatusEnum = BaseFunction.getEnum('BaseEnum.RowStatus');
        $scope.btnSave = function(){
            if($scope.Module.ScheduleRequest.FocusedRecord.Code == undefined || 
                $scope.Module.ScheduleRequest.FocusedRecord.Code.trim() == ""){
                BaseFunction.alert("编码不能为空！", "保存错误");
                return;
            }
            if($scope.Module.ScheduleRequest.FocusedRecord.Name == undefined || 
                $scope.Module.ScheduleRequest.FocusedRecord.Name.trim() == ""){
                BaseFunction.alert("名称不能为空！", "保存错误");
                return;
            }
            if($scope.Module.ScheduleRequest.FocusedRecord.ScheduleProgram == undefined || 
                $scope.Module.ScheduleRequest.FocusedRecord.ScheduleProgram == null){
                BaseFunction.alert("调度方案不能为空！", "保存错误");
                return;
            }
            if($scope.Module.ScheduleRequest.FocusedRecord.Scheduler == undefined || 
                $scope.Module.ScheduleRequest.FocusedRecord.Scheduler == null){
                BaseFunction.alert("调度程序不能为空！", "保存错误");
                return;
            }

            $scope.Module.ScheduleRequest.save();
        };
        $scope.btnCancel = function(){
            $scope.Module.ScheduleRequest.cancel();            
        };
        $scope.btnAdd = function(){
            $scope.Module.ScheduleRequest.add();
        };
        $scope.btnDelete = function(){
            $scope.Module.ScheduleRequest.delete();
        };
        $scope.btnList = function(){
            $location.url('/index/ScheduleRequest/List');
        };

        $scope.btnSubmitRequest = function(){
            var scheduleRequestIds = [$scope.Module.ScheduleRequest.FocusedRecord._id];
            BaseFunction.request('/api/Common/submitRequests', false, {ScheduleRequestIds: scheduleRequestIds}, 
                function(records){
                    records[0].RowStatus = $scope.RowStatusEnum.UnChange.Value;
                    $scope.Module.ScheduleRequest.FocusedRecord = records[0];
            });
        };

        $scope.btnCancelRequest = function(){
            var cancelRequestIds = [$scope.Module.ScheduleRequest.FocusedRecord._id];
            BaseFunction.request('/api/Common/cancelRequests', false, {CancelRequestIds: cancelRequestIds}, 
                function(records){
                    records[0].RowStatus = $scope.RowStatusEnum.UnChange.Value;
                    $scope.Module.ScheduleRequest.FocusedRecord = records[0];
            });
        };

        $scope.btnMonitorInfo = function(){
            if(!$scope.Module.ScheduleMonitor.Condition || $scope.Module.ScheduleMonitor.Condition.ScheduleRequest != $scope.Module.ScheduleRequest.FocusedRecord._id){
                $scope.Module.ScheduleMonitor.Condition = {ScheduleRequest: $scope.Module.ScheduleRequest.FocusedRecord._id};
                $scope.Module.ScheduleMonitor.find($scope.Module.ScheduleMonitor.Condition, {StartTime: 1}, null, function(records){
                    $scope.Module.ScheduleMonitor.Records = records;
                    $scope.Module.ScheduleMonitor.emitOnLoadEvent();
                });
            }            
        };

        //初始化
        BaseModule.initModule($scope, 'ScheduleRequest');
        $scope.Module.ScheduleRequest.Populate = [{path: 'Scheduler', select: 'Code Name'}, {path: 'ScheduleProgram', select: 'Code Name'}];
        $scope.Module.ScheduleRequest.AfterOnAdd = function(record){
            record.Status = $scope.ScheduleStatusEnum.No.Value;
        };

        BaseModule.initModule($scope, 'ScheduleMonitor');
        $scope.Module.ScheduleMonitor.PageSize = 10;

        if($stateParams.id) {
            $scope.Module.ScheduleRequest.findById($stateParams.id, function(record){
                $scope.Module.ScheduleRequest.FocusedRecord = record;
            });
        }
        else{
            $scope.btnAdd();
        };
    }
]);
